package com.company.cloud.mall.modules.app.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

import com.company.cloud.common.utils.LocalDateTimeUtils;
import com.company.cloud.mall.modules.api.constant.OrderRefundStateEnum;
import com.company.cloud.mall.modules.api.constant.OrderReturnStateEnum;
import com.company.cloud.mall.modules.api.response.OrderAfterModel;
import com.company.cloud.mall.modules.base.entity.OrderRefunds;
import com.company.cloud.mall.modules.base.entity.OrderReturns;

import cn.hutool.core.bean.BeanUtil;

public class AfterOrderUtil {

	/**
	 * 验证售后订单是否进行中
	 */
	public static boolean validateOrderIng(List<OrderRefunds> orderRefundsList, 
			List<OrderReturns> orderReturnsList) {
		// 退款
		if (!CollectionUtils.isEmpty(orderRefundsList)) {
			for (OrderRefunds refundItem : orderRefundsList) {
				if (OrderRefundStateEnum.ORFS_Check_Fail.getCode().equals(refundItem.getState())
						|| OrderRefundStateEnum.ORFS_Complete.getCode().equals(refundItem.getState())
						|| OrderRefundStateEnum.ORFS_Cancel.getCode().equals(refundItem.getState())) {
				} else {
					return false;
				}
			}
		}
		// 退款退货
		if (!CollectionUtils.isEmpty(orderReturnsList)) {
			for (OrderReturns returnItem : orderReturnsList) {
				if (OrderReturnStateEnum.ORFPS_Check_Fail.getCode().equals(returnItem.getState())
						|| OrderReturnStateEnum.ORFPS_Complete.getCode().equals(returnItem.getState())
						|| OrderReturnStateEnum.ORFPS_Cancel.getCode().equals(returnItem.getState())) {
				} else {
					return false;
				}
			}
		}
		return true;
	}
	
	
	/**
	 * 根据订单明细取最新的售后记录
	 * */
	public static Map<Integer, OrderAfterModel> getLastAfterByOrderItem(List<OrderRefunds> orderRefundsList,List<OrderReturns> orderReturnsList){
		Map<Integer, OrderAfterModel> map=new HashMap<Integer, OrderAfterModel>();
		if(!CollectionUtils.isEmpty(orderRefundsList)) {
			orderRefundsList.forEach(model->{
				OrderAfterModel oam= map.get(model.getOrderItemId());
				if(oam==null) {
					map.put(model.getOrderItemId(), AfterOrderUtil.getOrderAfterModel(model));
				}else {
					if(LocalDateTimeUtils.compare(model.getCdt(), oam.getCdt())>0) {
						map.put(model.getOrderItemId(), AfterOrderUtil.getOrderAfterModel(model));
					}
				}
			});
		}
		if(!CollectionUtils.isEmpty(orderReturnsList)) {
			orderReturnsList.forEach(model->{
				OrderAfterModel oam= map.get(model.getOrderItemId());
				if(oam==null) {
					map.put(model.getOrderItemId(), AfterOrderUtil.getOrderAfterModel(model));
				}else {
					if(LocalDateTimeUtils.compare(model.getCdt(), oam.getCdt())>0) {
						map.put(model.getOrderItemId(), AfterOrderUtil.getOrderAfterModel(model));
					}
				}
			});
		}
		return map;
	}
	
	
	public static OrderAfterModel getOrderAfterModel(OrderRefunds entity) {
		OrderAfterModel model = new OrderAfterModel();
		BeanUtil.copyProperties(entity, model);
		model.setType("1");
		return model;
	}

	public static OrderAfterModel getOrderAfterModel(OrderReturns entity) {
		OrderAfterModel model = new OrderAfterModel();
		BeanUtil.copyProperties(entity, model);
		model.setType("2");
		return model;
	}

	
	

}
